/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.PA165.musiclibrarybackend.services;

import cz.muni.fi.PA165.musiclibraryapi.dto.SongDTOInterface;
import cz.muni.fi.PA165.musiclibraryapi.services.SongService;
import cz.muni.fi.PA165.musiclibrarybackend.dao.SongDAO;
import cz.muni.fi.PA165.musiclibrarybackend.entities.Song;
import cz.muni.fi.PA165.musiclibrarybackend.transfers.SongT;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Tibor
 */
@Service
@Transactional
public class SongServiceImpl implements SongService, Serializable {

    private SongDAO songDAO;
    
    public void setSongDAO(SongDAO songDAO) {
        this.songDAO = songDAO;
    }
    @Transactional(propagation= Propagation.REQUIRED)
    public void createSong(SongDTOInterface song) {
        songDAO.createSong(SongT.DTOtoSong(song));
    }

    @Transactional(readOnly=true)
    public SongDTOInterface getSong(Long id) {
        return SongT.SongToDTO(songDAO.getSong(id));
    }

    @Transactional(readOnly=true)
    public List<SongDTOInterface> getAllSongs() {
        List<SongDTOInterface> allSongsDTO = new ArrayList();
        List<Song> allSongs = songDAO.getAllSongs();
        for(Song song : allSongs) {
            allSongsDTO.add(SongT.SongToDTO(song));
        }
        return allSongsDTO;
    }

    public void updateSong(SongDTOInterface song) {
        songDAO.updateSong(SongT.DTOtoSong(song));
    }

    public void deleteSong(SongDTOInterface song) {
        songDAO.deleteSong(SongT.DTOtoSong(song));
    }
    
}
